package config

import (
	"common/helper"
	"database/sql"
	"sync"
)

var HcmGameListCfg sync.Mutex

type GameListCfg struct {
	GameId     string `db:"game_id" json:"game_id"`
	PlatformId int    `db:"platform_id" json:"platform_id"`
	GameType   int    `db:"game_type" json:"game_type"`
	Maintained int    `db:"maintained" json:"maintained"`
	ToFake     int    `db:"to_fake" json:"to_fake"`
	Online     int    `db:"online" json:"online"`
}

var G_GameList []GameListCfg

var G_GameType map[int]string = make(map[int]string)

func CopyGameType() map[int]string {
	HcmGameListCfg.Lock()
	defer HcmGameListCfg.Unlock()

	res := map[int]string{}
	for key, val := range G_GameType {
		res[key] = val
	}

	return res
}
func GameListCfgRemove() {
	HcmGameListCfg.Lock()
	defer HcmGameListCfg.Unlock()
	G_GameList = []GameListCfg{}
}

func GameListCfgSetRow(row *sql.Rows) {
	var tVal GameListCfg
	var err error
	err = row.Scan(&tVal.GameId, &tVal.PlatformId, &tVal.GameType, &tVal.Maintained, &tVal.ToFake, &tVal.Online)
	if err != nil {
		helper.InfoLog("GameListCfgSetRow,scan err")
		return
	}

	HcmGameListCfg.Lock()
	defer HcmGameListCfg.Unlock()
	G_GameList = append(G_GameList, tVal)
	G_GameType[tVal.PlatformId] = "1"

	//确保某些gameid  tbl_game_lists表中找不到 返回大类ID 就不会被统计
	G_GameType[tVal.PlatformId/10*10] = "1"
}

func FindPlatformByGamePP(gameId string) int {
	HcmGameListCfg.Lock()
	defer HcmGameListCfg.Unlock()
	for _, value := range G_GameList {
		if value.GameId == gameId && (value.PlatformId >= 200 && value.PlatformId < 400) {
			return value.PlatformId
		}
	}

	return 301
}

func FindPlatformByGame(gameId string, PlatformId int) int {
	HcmGameListCfg.Lock()
	defer HcmGameListCfg.Unlock()
	for _, value := range G_GameList {
		if value.GameId == gameId && value.PlatformId/10*10 == PlatformId {
			return value.PlatformId
		}
	}
	return PlatformId
}

func FindJuhePlatByGame(gameId string) int {
	HcmGameListCfg.Lock()
	defer HcmGameListCfg.Unlock()
	for _, value := range G_GameList {
		if value.GameId == gameId {
			return value.PlatformId
		}
	}
	return 0
}

func FindGameMaintainedState(gameId string) int {
	HcmGameListCfg.Lock()
	defer HcmGameListCfg.Unlock()
	for _, value := range G_GameList {
		if value.GameId == gameId {
			return value.Maintained
		}
	}
	return 2
}

// 是否切换到假游戏
func IsSwitchToFake(gameId string, PlatformId int) int {
	HcmGameListCfg.Lock()
	defer HcmGameListCfg.Unlock()
	for _, value := range G_GameList {
		if value.GameId == gameId && value.PlatformId == PlatformId {
			return value.ToFake
		}
	}
	return 0
}
func FindGameOnlineState(gameId string) int {
	HcmGameListCfg.Lock()
	defer HcmGameListCfg.Unlock()
	for _, value := range G_GameList {
		if value.GameId == gameId {
			return value.Online
		}
	}
	return 2
}
